package leetcode.code0856;

import java.util.Stack;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int scoreOfParentheses(String s) {
		char[] cs = s.toCharArray();
		Stack<int[]> s1 = new Stack<>();
		for (int i = 0; i < cs.length; i++) {
			int c = cs[i];
			if (c == (int) '(') {
				s1.add(new int[] { 0 });
			} else {
				if (s1.peek()[0] == 0) {
					s1.pop();
					s1.add(new int[] { 1, 1 });
				} else {
					int num = 0;
					while (!s1.isEmpty() && s1.peek()[0] == 1) {
						num += s1.pop()[1];
					}
					s1.pop();
					num <<= 1;
					s1.add(new int[] { 1, num });
				}
			}
		}
		int ans = 0;
		while (!s1.isEmpty() && s1.peek()[0] == 1) {
			ans += s1.pop()[1];
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		System.out.println((int) '(');
	}

}
